import { Router } from 'express';
import { authenticate, authorize } from '../../middleware/auth';
import { SystemController } from '../../admin/controllers/systemController';
import { logOperation, skipLog } from '../../middleware/operationLog';
import { ALL_PERMISSIONS } from '../../constants/permissions';

const router = Router();
const systemController = new SystemController();

/**
 * @swagger
 * /api/admin/system/configs:
 *   get:
 *     summary: 获取系统配置列表
 *     description: 分页获取系统配置列表，支持搜索和类型过滤
 *     tags: [系统管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 20
 *         description: 每页数量
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: 搜索关键词（配置键或描述）
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [STRING, NUMBER, BOOLEAN, JSON]
 *         description: 配置类型过滤
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     configs:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/SystemConfig'
 *                     pagination:
 *                       $ref: '#/components/schemas/Pagination'
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器内部错误
 */
router.get('/configs', authenticate, authorize([ALL_PERMISSIONS.SYSTEM_CONFIG_READ]), skipLog, systemController.getSystemConfigs);

/**
 * @swagger
 * /api/admin/system/configs:
 *   post:
 *     summary: 创建系统配置
 *     description: 创建新的系统配置项
 *     tags: [系统管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - key
 *               - value
 *             properties:
 *               key:
 *                 type: string
 *                 minLength: 1
 *                 maxLength: 100
 *                 description: 配置键（唯一标识）
 *                 example: "site_name"
 *               value:
 *                 type: string
 *                 description: 配置值
 *                 example: "我的系统"
 *               description:
 *                 type: string
 *                 maxLength: 500
 *                 description: 配置描述
 *                 example: "网站名称"
 *               type:
 *                 type: string
 *                 enum: [STRING, NUMBER, BOOLEAN, JSON]
 *                 default: STRING
 *                 description: 配置类型
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   $ref: '#/components/schemas/SystemConfig'
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       409:
 *         description: 配置键已存在
 *       500:
 *         description: 服务器内部错误
 */
router.post('/configs', authenticate, authorize([ALL_PERMISSIONS.SYSTEM_CONFIG_CREATE]), logOperation('创建系统配置', '系统管理'), systemController.createSystemConfig);

/**
 * @swagger
 * /api/admin/system/configs/batch:
 *   put:
 *     summary: 批量更新系统配置
 *     description: 批量更新多个系统配置项
 *     tags: [系统管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - configs
 *             properties:
 *               configs:
 *                 type: array
 *                 minItems: 1
 *                 items:
 *                   type: object
 *                   required:
 *                     - key
 *                     - value
 *                   properties:
 *                     key:
 *                       type: string
 *                       description: 配置键
 *                     value:
 *                       type: string
 *                       description: 配置值
 *                     description:
 *                       type: string
 *                       description: 配置描述
 *     responses:
 *       200:
 *         description: 批量更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     updatedCount:
 *                       type: integer
 *                       description: 更新成功的配置数量
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器内部错误
 */
router.put('/configs/batch', authenticate, authorize([ALL_PERMISSIONS.SYSTEM_CONFIG_UPDATE]), logOperation('批量更新系统配置', '系统管理'), systemController.batchUpdateSystemConfigs);

/**
 * @swagger
 * /api/admin/system/configs/{key}:
 *   get:
 *     summary: 获取指定配置
 *     description: 根据配置键获取单个系统配置
 *     tags: [系统管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 配置键
 *         example: "site_name"
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   $ref: '#/components/schemas/SystemConfig'
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 配置不存在
 *       500:
 *         description: 服务器内部错误
 */
router.get('/configs/:key', authenticate, authorize([ALL_PERMISSIONS.SYSTEM_CONFIG_READ]), skipLog, systemController.getSystemConfigByKey);

/**
 * @swagger
 * /api/admin/system/configs/{key}:
 *   put:
 *     summary: 更新指定配置
 *     description: 更新指定键的系统配置
 *     tags: [系统管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 配置键
 *         example: "site_name"
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - value
 *             properties:
 *               value:
 *                 type: string
 *                 description: 新的配置值
 *                 example: "新的网站名称"
 *               description:
 *                 type: string
 *                 description: 配置描述
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   $ref: '#/components/schemas/SystemConfig'
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 配置不存在
 *       500:
 *         description: 服务器内部错误
 */
router.put('/configs/:key', authenticate, authorize([ALL_PERMISSIONS.SYSTEM_CONFIG_UPDATE]), logOperation('更新系统配置', '系统管理'), systemController.updateSystemConfig);

/**
 * @swagger
 * /api/admin/system/configs/{key}:
 *   delete:
 *     summary: 删除指定配置
 *     description: 删除指定键的系统配置
 *     tags: [系统管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: key
 *         required: true
 *         schema:
 *           type: string
 *         description: 配置键
 *         example: "site_name"
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 message:
 *                   type: string
 *                   example: "配置删除成功"
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 配置不存在
 *       500:
 *         description: 服务器内部错误
 */
router.delete('/configs/:key', authenticate, authorize([ALL_PERMISSIONS.SYSTEM_CONFIG_DELETE]), logOperation('删除系统配置', '系统管理'), systemController.deleteSystemConfig);

/**
 * @swagger
 * /api/admin/system/stats:
 *   get:
 *     summary: 获取系统统计信息
 *     description: 获取系统运行状态、用户统计、配置统计等信息
 *     tags: [系统管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     systemInfo:
 *                       type: object
 *                       properties:
 *                         version:
 *                           type: string
 *                           description: 系统版本
 *                         uptime:
 *                           type: number
 *                           description: 运行时间（秒）
 *                         memoryUsage:
 *                           type: object
 *                           description: 内存使用情况
 *                         cpuUsage:
 *                           type: object
 *                           description: CPU使用情况
 *                     userStats:
 *                       type: object
 *                       properties:
 *                         totalUsers:
 *                           type: integer
 *                           description: 总用户数
 *                         activeUsers:
 *                           type: integer
 *                           description: 活跃用户数
 *                         newUsersToday:
 *                           type: integer
 *                           description: 今日新增用户
 *                     configStats:
 *                       type: object
 *                       properties:
 *                         totalConfigs:
 *                           type: integer
 *                           description: 总配置数
 *                         configTypes:
 *                           type: object
 *                           description: 各类型配置数量
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器内部错误
 */
router.get('/stats', authenticate, authorize([ALL_PERMISSIONS.SYSTEM_STATS_READ]), skipLog, systemController.getSystemStats);

/**
 * @swagger
 * /api/admin/system/cache/clear:
 *   post:
 *     summary: 清除缓存
 *     description: 清除指定类型或前缀的Redis缓存
 *     tags: [系统管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - type
 *             properties:
 *               type:
 *                 type: string
 *                 enum: [all, prefix, pattern]
 *                 description: 清除类型
 *                 example: "prefix"
 *               prefix:
 *                 type: string
 *                 description: 缓存前缀（当type为prefix时必填）
 *                 example: "user"
 *     responses:
 *       200:
 *         description: 清除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     message:
 *                       type: string
 *                       example: "缓存清除成功"
 *                     type:
 *                       type: string
 *                       example: "prefix"
 *                     prefix:
 *                       type: string
 *                       example: "user"
 *                     clearedCount:
 *                       type: integer
 *                       example: 3
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器内部错误
 */
router.post('/cache/clear', authenticate, authorize([ALL_PERMISSIONS.SYSTEM_CACHE_CLEAR]), logOperation('清除缓存', '系统管理'), systemController.clearCache);

/**
 * @swagger
 * /api/admin/system/cache/stats:
 *   get:
 *     summary: 获取缓存统计信息
 *     description: 获取Redis缓存的统计信息，包括键数量、内存使用等
 *     tags: [系统管理]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     stats:
 *                       type: object
 *                       properties:
 *                         totalKeys:
 *                           type: integer
 *                           description: 总键数量
 *                         memoryUsage:
 *                           type: string
 *                           description: 内存使用量
 *                         connectedClients:
 *                           type: integer
 *                           description: 连接客户端数
 *                         uptime:
 *                           type: number
 *                           description: 运行时间
 *                     config:
 *                       type: object
 *                       properties:
 *                         host:
 *                           type: string
 *                           description: Redis主机
 *                         port:
 *                           type: integer
 *                           description: Redis端口
 *                         database:
 *                           type: integer
 *                           description: 数据库编号
 *                         enabled:
 *                           type: boolean
 *                           description: 是否启用
 *                     health:
 *                       type: object
 *                       properties:
 *                         status:
 *                           type: string
 *                           example: "healthy"
 *                         timestamp:
 *                           type: string
 *                           format: date-time
 *                           example: "2024-01-01T00:00:00.000Z"
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器内部错误
 */
router.get('/cache/stats', authenticate, authorize([ALL_PERMISSIONS.SYSTEM_CACHE_READ]), skipLog, systemController.getCacheStats);

/**
 * @swagger
 * /api/admin/system/cache/warmup:
 *   post:
 *     summary: 预热缓存
 *     description: 预热指定类型的缓存数据
 *     tags: [系统管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - type
 *             properties:
 *               type:
 *                 type: string
 *                 enum: [system_config, user_permissions]
 *                 description: 预热类型
 *                 example: "system_config"
 *     responses:
 *       200:
 *         description: 预热成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     message:
 *                       type: string
 *                       example: "缓存预热成功"
 *                     type:
 *                       type: string
 *                       example: "system_config"
 *                     warmedCount:
 *                       type: integer
 *                       example: 10
 *                     warmedItems:
 *                       type: array
 *                       items:
 *                         type: string
 *                       example: ["site_name", "site_description"]
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器内部错误
 */
router.post('/cache/warmup', authenticate, authorize([ALL_PERMISSIONS.SYSTEM_CACHE_WARMUP]), logOperation('预热缓存', '系统管理'), systemController.warmupCache);

/**
 * @swagger
 * /api/admin/system/cache/keys:
 *   get:
 *     summary: 获取缓存键列表
 *     description: 获取Redis缓存的键列表，支持模式匹配和分页
 *     tags: [系统管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: pattern
 *         schema:
 *           type: string
 *           default: "*"
 *         description: 键模式匹配
 *         example: "user:*"
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 1000
 *           default: 100
 *         description: 返回键数量限制
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     pattern:
 *                       type: string
 *                       example: "user:*"
 *                     totalKeys:
 *                       type: integer
 *                       description: 总键数量
 *                       example: 150
 *                     returnedKeys:
 *                       type: integer
 *                       description: 返回键数量
 *                       example: 100
 *                     keys:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           key:
 *                             type: string
 *                             example: "user:1"
 *                           type:
 *                             type: string
 *                             example: "string"
 *                           ttl:
 *                             type: integer
 *                             example: 3600
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器内部错误
 */
router.get('/cache/keys', authenticate, authorize([ALL_PERMISSIONS.SYSTEM_CACHE_READ]), skipLog, systemController.getCacheKeys);

export default router; 